package com.chookss.tools.filePicker.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.chookss.R;
import com.chookss.tools.MyToast;
import com.chookss.tools.filePicker.SelectOptions;
import com.chookss.tools.filePicker.loader.EssMimeTypeCollection;
import com.chookss.tools.filePicker.model.EssFile;
import com.chookss.tools.filePicker.model.FileScanActEvent;
import com.chookss.tools.filePicker.model.FileScanFragEvent;
import com.chookss.tools.filePicker.util.Const;
import com.google.android.material.tabs.TabLayout;
import com.lvgroup.hospital.base.BaseAct;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * 通过扫描来选择文件
 */
public class SelectFileByScanActivity extends BaseAct implements ViewPager.OnPageChangeListener {

    /*todo 是否可预览文件，默认可预览*/
    private boolean mCanPreview = true;

    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private LinearLayout title_back;
    private TextView tvNum;

    private ArrayList<EssFile> mSelectedFileList = new ArrayList<>();
    /*当前选中排序方式的位置*/
    private int mSelectSortTypeIndex = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_file_by_scan);
        initUi();
    }


    private void initUi() {
        initState3();
        mViewPager = findViewById(R.id.vp_select_file_scan);
        mTabLayout = findViewById(R.id.tabl_select_file_scan);
        tvNum = findViewById(R.id.tvNum);
        title_back = findViewById(R.id.title_back);
        mTabLayout.setTabGravity(TabLayout.GRAVITY_CENTER);
        mTabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        tvNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mSelectedFileList.size() == 0) {
                    MyToast.show("最少选择一个文件");
                } else {
                    Intent result = new Intent();
                    result.putParcelableArrayListExtra(Const.EXTRA_RESULT_SELECTION, mSelectedFileList);
                    setResult(RESULT_OK, result);
                    finish();
                }
            }
        });
        title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        List<Fragment> fragmentList = new ArrayList<>();
        for (int i = 0; i < SelectOptions.getInstance().getFileTypes().length; i++) {
            fragmentList.add(FileTypeListFragment.newInstance(SelectOptions.getInstance().getFileTypes()[i], SelectOptions.getInstance().isSingle, SelectOptions.getInstance().maxCount, 3, EssMimeTypeCollection.LOADER_ID + i));
        }
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager(), fragmentList, Arrays.asList(SelectOptions.getInstance().getFileTypes()));
        mViewPager.setAdapter(adapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mViewPager.setOffscreenPageLimit(fragmentList.size() - 1);
        mViewPager.addOnPageChangeListener(this);

    }


    /**
     * Fragment中选择文件后
     *
     * @param event event
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFragSelectFile(FileScanFragEvent event) {
        if (event.isAdd()) {
            mSelectedFileList.add(event.getSelectedFile());
        } else {
            mSelectedFileList.remove(event.getSelectedFile());
        }
        if(mSelectedFileList.size()==0){
            tvNum.setText("确定");
        }else{
            tvNum.setText("确定(" + mSelectedFileList.size() + ")");
        }
        EventBus.getDefault().post(new FileScanActEvent(SelectOptions.getInstance().maxCount - mSelectedFileList.size()));
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        EventBus.getDefault().post(new FileScanActEvent(SelectOptions.getInstance().maxCount - mSelectedFileList.size()));
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void initData() {
        initState();
    }

}
